/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.great.ad;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.ufc.great.ad.DAO.UserDAO;
import br.ufc.great.ad.Model.User;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.ufc.great.ad.DAO.UserDAO;
import br.ufc.great.ad.Model.DB;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author great
 */
@Resource
public class UserController {

    Validator validator;
    UserDAO userDAO = new UserDAO();
    private Result result;

    public UserController(Result result, Validator validator) {
        this.result = result;
        this.validator = validator;
    }

    public void alterUser(User userToEdit) {
        UserDAO userDAO = new UserDAO();
        userDAO.editUser(userToEdit);
    }

    @Post
    public void addUser(User user, String pass2) {
        //INICIO LÓGICA VALIDAÇÃO
        if (user.getName().equals("")) {
            validator.add(new ValidationMessage("Complete the field Name.", "erroAddUser"));
        }

        if (user.getEmail().equals("")) {
            validator.add(new ValidationMessage("Complete the field Email.", "erroAddUser"));
        } else if (!user.getEmail().contains("@")) {
            validator.add(new ValidationMessage("This email is invalid.", "erroAddUser"));
        }

        if (user.getLogin().equals("")) {
            validator.add(new ValidationMessage("Complete the field Login.", "erroAddUser"));
        }
        if (!user.getPass().equals(pass2)) {
            validator.add(new ValidationMessage("Complete the fields with the same password.", "erroAddUser"));
        }
        //FIM LÓGICA VALIDAÇÃO
        validator.onErrorUsePageOf(UserController.class).addUser();
        userDAO.addUser(user);

        result.redirectTo("signSucess");
    }

    @Get
    public void addUser() {
    }

    @Get
    public void editUser() {
    }

    @Post
    public void editUser(User user, String pass2) {
        User userToEdit = user;
        //INICIO LÓGICA VALIDAÇÃO
        if (userToEdit.getName().equals("")) {
            validator.add(new ValidationMessage("Complete the field Name.", "erroAddUser"));
        }

        if (userToEdit.getEmail().equals("")) {
            validator.add(new ValidationMessage("Complete the field Email.", "erroAddUser"));
        } else if (!userToEdit.getEmail().contains("@")) {
            validator.add(new ValidationMessage("This email is invalid.", "erroAddUser"));
        }

        if (userToEdit.getLogin().equals("")) {
            validator.add(new ValidationMessage("Complete the field Login.", "erroAddUser"));
        }
        if (!userToEdit.getPass().equals(pass2)) {
            validator.add(new ValidationMessage("Complete the fields with the same password.", "erroAddUser"));
        }
        //FIM LÓGICA VALIDAÇÃO
        validator.onErrorUsePageOf(UserController.class).editUser();
        userDAO.editUser(userToEdit); //retorna null se o usuário nao for encontrado no banco
        result.include("user", userToEdit);
        result.redirectTo("../user/updateSucess");
    }

    public void updateSucess() {
    }
    
    public void signSucess(){
        result.redirectTo("../login/index");
    }
}
